#!/usr/bin/python
#-*- coding: utf-8 -*-

# CPBMI Python Homework 3-4
# Programmed by Seunghwan Kim

# 1)

centerA = dict()
centerB = dict()

# 2)

fp = open("centerA_result.txt")
for line in fp.readlines():
    if line.startswith('#') : continue
    data = line.split('\t')
    centerA[data[0]] = float(data[1].strip())
fp.close()

fp = open("centerB_result.txt")
for line in fp.readlines():
    if line.startswith('#') : continue
    data = line.split('\t')
    centerB[data[0]] = float(data[1].strip())
fp.close()

# 3)

print "Number of symbols in Center A is %d" % len(centerA)
print "Number of symbols in Center B is %d" % len(centerB)

# 4)

print "Common symbols:",

setA = set(centerA.keys())
setB = set(centerB.keys())

for isym in setA.intersection(setB):
    print isym,
print

# 5)

print "Different symbols:",

for dsym in setA.symmetric_difference(setB):
    print dsym,
print

# 6)

print "Average fold change ratio of NBR1 : %.1f" % ((centerA['NBR1'] + centerB['NBR1']) / 2)
print "Average fold change ratio of RPL23P6 : %.1f" % ((centerA['RPL23P6'] + centerB['RPL23P6']) / 2)

# 7)

fout = open("gene_intersection.txt", "w")
for sym in setA.intersection(setB):
    fout.write("%s\t%.2f\n" % (sym, (centerA[sym] + centerB[sym]) / 2))
fout.close()
